/*
 *
 * Copyright(c) 2010 Safelayer Secure Communications
 *
 * This is open source software, placed under the terms of the
 * GNU GENERAL PUBLIC LICENSE Version 3.
 * Please see: http://www.gnu.org/licenses/gpl-3.0.txt
 * 
 * Permission to use, copy, modify, and distribute this software
 * for any purpose and without fee is hereby granted, provided
 * that the above copyright notices appear in all copies and that
 * both the copyright notice and this permission notice appear in
 * supporting documentation.
 * 
 */
package com.neuralnoise.foaf.core;

import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;

/**
 * FirstName based smushing FOAF rule.
 * 
 * @author victor.martinez <swtp@safelayer.com>
 * 
 */
public class GivenNameMatchRule extends Rule {

	private Property givenNameProperty;

	private Property familyNameProperty;

	/**
	 * This rules match all possible names pairs combinations.
	 */

	public GivenNameMatchRule() {
		super("Names Match");
		givenNameProperty = FOAFVocabulary.givenName;
		familyNameProperty = FOAFVocabulary.familyName;
	}

	@Override
	public boolean equals(Resource resource1, Resource resource2) {

		if (!resource1.hasProperty(givenNameProperty) || !resource2.hasProperty(givenNameProperty)
				|| !resource1.hasProperty(familyNameProperty) || !resource2.hasProperty(familyNameProperty))
			return false;
		// get givenName and familyName from resources
		String firstNameRes1 = resource1.getProperty(givenNameProperty).getLiteral().toString();
		String familyNameRes1 = resource1.getProperty(familyNameProperty).getLiteral().toString();
		String firstNameRes2 = resource2.getProperty(givenNameProperty).getLiteral().toString();
		String familyNameRes2 = resource2.getProperty(familyNameProperty).getLiteral().toString();
		// construct names
		String composedName1 = firstNameRes1 + familyNameRes1;
		String composedName2 = firstNameRes2 + familyNameRes2;
		return composedName1.equals(composedName2);
	}

	@Override
	public boolean isTrustEquals() {
		return true;
	}

}
